package cn.kgc.Adjudicative.controller;


import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.kgc.Adjudicative.common.Result;
import cn.kgc.Adjudicative.entity.DBook;
import cn.kgc.Adjudicative.service.DBookService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sun
 * @since 2023-01-05
 */
@RestController
@RequestMapping("/d-book")
public class DBookController {

    @Autowired
    private DBookService bookService;

    // @RequiresPermissions("user:*")
    @GetMapping("/pageNotOnline")
    public Result findPageNotOnline(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize,
                                    @RequestParam Integer isInternet,
                                    @RequestParam String bookName,
                                    @RequestParam String caseno,
                                    @RequestParam String undertaker,
                                    @RequestParam(required = false) String endTime){
        QueryWrapper<DBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isInternet",isInternet);
        if (!bookName.equals("")){
            queryWrapper.like("bookName",bookName);
        }
        if (!caseno.equals("")){
            queryWrapper.like("caseNo",caseno);
        }
        if (!undertaker.equals("")){
            queryWrapper.like("undertaker",undertaker);
        }
        if (!endTime.equals("")){
            queryWrapper.like("endTime",endTime);
        }
        Page<DBook> page = bookService.page(new Page<>(pageNum,pageSize),queryWrapper);
        System.out.println(page);
        return Result.success(page);
    }

    @GetMapping("/pageStatus")
    public Result findPageStatus(@RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize,
                                 @RequestParam Integer status){
        QueryWrapper<DBook> queryWrapper = new QueryWrapper<>();
        if (status.equals(1)){
            List<Integer> list = new ArrayList<>();
            list.add(1);
            list.add(0);
            queryWrapper.in("status",list);
        }
        if (status.equals(2)){
            queryWrapper.eq("status",status);
        }
        Page<DBook> page = bookService.page(new Page<>(pageNum,pageSize),queryWrapper);
        System.out.println(page);
        return Result.success(page);
    }

    /**
     * 导出接口
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        //从数据库中查询所有的数据
        List<DBook> list = bookService.list();
        //通过工具类创建writer写出到磁盘路径
        // ExcelWriter writer = ExcelUtil.getWriter(filesUploadPath + "/用户信息.xlsx");
        //在内存操作，写到浏览器
        ExcelWriter writer = ExcelUtil.getWriter(true);
        // Word07Writer writer = new Word07Writer();
        //自定义标题别名
        writer.addHeaderAlias("id","id");
        writer.addHeaderAlias("username","用户名");
        writer.addHeaderAlias("password","密码");
        writer.addHeaderAlias("nickname","昵称");
        writer.addHeaderAlias("email","邮箱");
        writer.addHeaderAlias("phone","电话");
        writer.addHeaderAlias("address","地址");
        writer.addHeaderAlias("createTime","创建时间");
        writer.addHeaderAlias("avatarUrl","头像");

        // 一次性写出list内的对象到excel，使用默认样式，强制输出标题
        writer.write(list, true);

        // 设置浏览器响应的格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("文书信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        out.close();
        writer.close();
    }
}
